package com.wxq.workpad

import android.R
import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.StrictMode
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator
import com.tencent.mmkv.MMKV
import com.wxq.workpad.utils.ApplicationUtils
import com.wxq.workpad.utils.autosize.AutoSizeHelper
import com.wxq.workpad.utils.log.LogCatStrategy
import com.wxq.workpad.utils.log.LogUtils
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener


class MyApp : Application() {

        //设置全局的Header构建器


    override fun onCreate() {
        super.onCreate()
        ApplicationUtils.init(this)
        initLooger()
        initAuToSize()
        ignoreShareRule()
        MMKV.initialize(this)
        initSmart();
    }

    private fun initSmart() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(object: DefaultRefreshHeaderCreator {
            override fun createRefreshHeader(
                context: Context,
                layout: RefreshLayout
            ): RefreshHeader {
//                layout.setPrimaryColors()
              var header=  ClassicsHeader(context)
                header.setDrawableProgressSize(12f)
                header.setDrawableArrowSize(12f)

                header.setTextSizeTime(6f)
                header.setTextSizeTitle(6f)
                return header
            }
        })


        SmartRefreshLayout.setDefaultRefreshFooterCreator(object :DefaultRefreshFooterCreator{
            override fun createRefreshFooter(
                context: Context,
                layout: RefreshLayout
            ): RefreshFooter {
                var foot=  ClassicsFooter(context)
                foot.setDrawableProgressSize(12f)
                foot.setDrawableArrowSize(12f)
                foot.setTextSizeTitle(6f)

                return  foot
            }

        })


    }

    private fun initAuToSize() {
        // 兼容下报错：https://github.com/JessYanCoding/AndroidAutoSize/issues/168
        AutoSize.initCompatMultiProcess(this)
        AutoSizeConfig.getInstance().onAdaptListener = object : onAdaptListener {
            override fun onAdaptBefore(target: Any, activity: Activity) {
                AutoSizeHelper.resetScreenSize(activity)
            }

            override fun onAdaptAfter(target: Any, activity: Activity) {}
        }
    }

    private fun ignoreShareRule() {
        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        builder.detectFileUriExposure()
    }

    private fun initLooger() {

        LogUtils.initLog()
        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
            .showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
            .methodCount(2) // (Optional) How many method line to show. Default 2
            .methodOffset(1) // (Optional) Hides internal method calls up to offset. Default 5
            .logStrategy(LogCatStrategy()) // (Optional) Changes the log strategy to print out. Default LogCat
            .tag("QF Log") // (Optional) Global tag for every log. Default PRETTY_LOGGER
            .build()
        Logger.addLogAdapter(object : AndroidLogAdapter(formatStrategy) {
            override fun isLoggable(priority: Int, tag: String?): Boolean {
                return true
            }
        })

    }

}